<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/10
 * Time: 10:28
 */
namespace Admin\Controller;

class AdminController extends RightController
{

    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_admin");
            $params = session("QG_SEARCH_PARAMS_admin");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_admin");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_admin", null);
            session("QG_SEARCH_PARAMS_admin", null);
            session("QG_SEARCH_SIZE_admin", null);
            $pageSize = I("post.pageSize");
            $params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }

        $where .= " AND t.isDelete = '2'";
        // 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('admin')
            ->alias("t")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('admin')
            ->alias("t")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_admin", $where);
        session("QG_SEARCH_PARAMS_admin", $params);
        session("QG_SEARCH_SIZE_admin", $pageSize);

        // 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('lists', array(
            "page" => getDropdownList("pageSize" ,"" ,false),
            "isDisable" => getDropdownList('isDisable')
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 账号
        if (I('post.account')) {
            // 精确
            if (I('post.accountCondition') == 1) {
                $where .= " AND t.account='" . I("post.account") . "'";
            } else {
                $where .= " AND t.account LIKE '%" . I("post.account") . "%'";
            }
        }

        // 名称
        if (I('post.name')) {
            // 精确
            if (I('post.nameCondition') == 1) {
                $where .= " AND t.name='" . I("post.name") . "'";
            } else {
                $where .= " AND t.name LIKE '%" . I("post.name") . "%'";
            }
        }

        // 是否禁用
        if (I('post.isdisable')) {
            $where .= " AND t.isDisable='" . I("post.isdisable") . "'";
        }

        // 创建日期
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if(I('post.createtimeStart')){
                $where .= " AND t.createTime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if(I('post.createtimeEnd')){
                $where .= " AND t.createTime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        // 更新日期
        if (I('post.updatetimeStart') || I('post.updatetimeEnd')) {
            // 起始日期
            if(I('post.updatetimeStart')){
                $where .= " AND t.updateTime>='" . I("post.updatetimeStart") . "'";
            }
            // 结束日期
            if(I('post.updatetimeEnd')){
                $where .= " AND t.updateTime<='" . I("post.updatetimeEnd") . " 23:59:59'";
            }
        }

        return $where;
    }

    // 禁用管理员
    public function disable()
    {
        if (IS_AJAX) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            // flag
            $data['isDisable'] = "1";

            // 更新时间
            $data['updateTime'] = now_datetime();

            // 更新人
            $data['updateId'] = $this->getAccountId();

            // 更新数据
            $result = M('admin')->where(array("id" => $id))->save($data);

            // 如果成功
            if ($result) {
                $this->ajaxSuccess("禁用成功！");
            } else {
                $this->ajaxError("禁用失败！");
            }
        }
    }

    // 启用管理员
    public function enable()
    {
        if (IS_AJAX) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            // flag
            $data['isDisable'] = "2";

            // 更新时间
            $data['updateTime'] = now_datetime();

            // 更新人
            $data['updateId'] = $this->getAccountId();

            // 更新数据
            $result = M('admin')->where(array("id" => $id))->save($data);

            // 如果成功
            if ($result) {
                $this->ajaxSuccess("启用成功！");
            } else {
                $this->ajaxError("启用失败！");
            }
        }
    }

    // 编辑管理员
    public function edit()
    {
        if (IS_AJAX) {
            // 管理员ID
            $id = I("post.id");

            // 更新数据
            $data = array();

            // 密码
            $password = I("post.password");
            if (!empty($password)) {
                $data["password"] = md5($password);
            }

            // 名称
            $data["name"] = I("post.name");

            // 如果是管理员，权限自定义
            $data["rolePath"] = join(",", I("post.rolepath"));
            $data['updateTime'] = now_datetime();
            $data['updateId'] = $this->getAccountId();

            // 更新管理员
            $result = M("admin")
                ->where(array("id" => $id))
                ->save($data);

            if ($result) {
                $this->ajaxSuccess("更新管理员成功！");
            } else {
                $this->ajaxError("更新管理员失败！");
            }

        } else {
            // 管理员ID
            $id = intval(I("get.id"));

            //判断管理员是否存在
            $data = M("admin")
                ->alias("a")
                ->field("a.*")
                ->where(array("a.id" => $id))
                ->find();

            //判断管理员是否存在
            if (empty($data)) {
                $this->error("管理员不存在！");
            }

            $lists = array(
                "roleList" => $this->getRoleList('' ,$data['rolepath'])
            );
            $this->assign("lists", $lists);
            $this->assign("role", $this->getAccount()['role']);
            $this->assign("data", $data);
            $this->display();
        }
    }

    // 添加管理员
    public function add()
    {
        if (IS_AJAX) {
            //登录账号
            $account = I("post.account");

            //判断此账户是否存在
            $admin = M("admin")
                ->where(array("account" => $account))
                ->find();

            if ($admin) {
                $this->ajaxError("操作失败，该账户已存在！");
            }

            // 登录账户
            $data["account"] = $account;

            // 登录密码
            $data["password"] = md5(I("post.password"));

            // 名称
            $data["name"] = I("post.name");

            // 如果是管理员，权限自定义
            $data["role"] = I("post.role");
            $data["rolePath"] = join(",", I("post.rolepath"));

            $data['isDelete'] = 2;
            $data['isDisable'] = 2;
            $data['createTime'] = now_datetime();
            $data['updateTime'] = now_datetime();
            $data['createId'] = $this->getAccountId();
            $data['updateId'] = $this->getAccountId();

            // 添加管理员
            $result = M("admin")->add($data);

            if ($result) {
                $this->ajaxSuccess("添加成功");
            } else {
                $this->ajaxError("添加失败！");
            }
        } else {
            $lists = array(
                "roleList" => $this->getRoleList()
            );
            $this->assign("lists", $lists);
            $this->assign("role", $this->getAccount()['role']);
            $this->assign("rolePath", $this->getAccount()['rolePath']);
            $this->display();
        }
    }

    // 删除
    public function delete()
    {
        if (IS_AJAX) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            // flag
            $data['isDelete'] = "1";

            // 更新时间
            $data['updateTime'] = now_datetime();

            // 更新人
            $data['updateId'] = $this->getAccountId();

            // 更新数据
            $result = M('admin')->where(array("id" => $id))->save($data);

            // 如果成功
            if ($result) {
                $this->ajaxSuccess("禁用成功！");
            } else {
                $this->ajaxError("禁用失败！");
            }
        }
    }
}